﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using YArchitech.LIB.YJKControls;

namespace YJKDataLookAfter
{
	public partial class DlgAdminLogin : System.Windows.Forms.Form
	{
		public DlgAdminLogin()
		{
			this.InitializeComponent();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public DbUser User
		{
			get
			{
				return this.mUser;
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			try
			{
				if (BimServerDB.TestConnect(this.cbxServer.Text.Trim(), this.txtDbName.Text.Trim(), this.txtUserName.Text.Trim(), this.txtPwd.Text, "3336", false))
				{
					XmlUtils.WriteConfigFile(this.fileName, "Server", "value", this.cbxServer.Text.Trim());
					XmlUtils.WriteConfigFile(this.fileName, "Uid", "value", this.txtUserName.Text.Trim());
					XmlUtils.WriteConfigFile(this.fileName, "Pwd", "value", this.txtPwd.Text.Trim());
					if (this.txtName.Text.Trim() != "")
					{
						DbUser user = DbUser.GetUser(this.txtName.Text.Trim(), BimServerDB.Default);
						if (user == null)
						{
							MessageBox.Show("用户名输入错误。");
						}
						else if (!user.Password.Equals(DbUser.ToMd5(this.txtPass.Text)))
						{
							MessageBox.Show("用户密码输入错误。");
						}
						else
						{
							this.mUser = user;
							if (user.Status == UserStatus.Registered && (user.IsAdmin() || user.IsFamilyManager() || user.IsProjectManager()))
							{
								if (this.cbxAuto.CheckState == CheckState.Checked)
								{
									string text = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
									XmlUtils.WriteConfigFile(text, "AutoUser", "value", this.txtName.Text.Trim());
									XmlUtils.WriteConfigFile(text, "AutoPwd", "value", this.txtPass.Text);
								}
								else
								{
									string text2 = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
									XmlUtils.WriteConfigFile(text2, "AutoUser", "value", "");
									XmlUtils.WriteConfigFile(text2, "AutoPwd", "value", "");
								}
								base.DialogResult = DialogResult.OK;
							}
							else
							{
								MessageBox.Show("您的账号没有管理权限。");
							}
						}
					}
					else
					{
						MessageBox.Show("请输入正确的用户名。");
					}
				}
				else
				{
					MessageBox.Show("服务器链接失败。");
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btnConfig_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog(this);
		}

		private void autoLogin()
		{
			try
			{
				string text = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";
				string dataFromConfigFile = XmlUtils.GetDataFromConfigFile(text, "AutoUser", "value");
				string dataFromConfigFile2 = XmlUtils.GetDataFromConfigFile(text, "AutoPwd", "value");
				DbUser user = DbUser.GetUser(dataFromConfigFile, BimServerDB.Default);
				if (user != null && !user.Password.Equals(dataFromConfigFile2))
				{
					this.mUser = user;
					base.DialogResult = DialogResult.OK;
				}
			}
			catch (Exception)
			{
			}
		}

		private void DlgLogin_Load(object sender, EventArgs e)
		{
			try
			{
				if (!new FileInfo(this.fileName).Exists)
				{
					XmlUtils.CreateConfigFile(this.fileName);
				}
				this.cbxServer.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Server", "value");
				this.txtUserName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Uid", "value");
				this.txtPwd.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "Pwd", "value");
				this.txtDbName.Text = XmlUtils.GetDataFromConfigFile(this.fileName, "DbName", "value");
				this.autoLogin();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void button1_Click_1(object sender, EventArgs e)
		{
		}

		private string fileName = AssistFunc.GetAppRootPath() + "\\Data\\Config.xml";

		public DbUser mUser;
	}
}
